﻿using MediatR;
using Traceability_Management.Domain;
using Traceability_Management.ErrorCode;
using Traceability_Management.Infrastructure.Interfaces;
using Traceability_Management.Read.Api.Application.Command.HealthyWay;

namespace Traceability_Management.Read.Api.Application.CommandHandler.HealthyWay
{
    public class UpdateHealthyCommandHandler : IRequestHandler<UpdateHealthyCommand, ApiResult<int>>
    {
        IBaseReponsitory<Healthys> Healthys;

        public UpdateHealthyCommandHandler(IBaseReponsitory<Healthys> Healthys)
        {
            this.Healthys = Healthys;
        }

        public async Task<ApiResult<int>> Handle(UpdateHealthyCommand request, CancellationToken cancellationToken)
        {
            var list = Healthys.GetQuery().Where(c => c.Healthys_Way == request.Healthys.Healthys_Way && !c.IsDel && c.Healthys_Id != request.Healthys.Healthys_Id).ToList();
            if (list.Count > 0)
            {
                return new ApiResult<int>
                {
                    Code = ApiEnum.False,
                    Message = "该方式不可重复",
                    Data = -1
                };
            }
            await Healthys.UpdateAsync(request.Healthys);
            return new ApiResult<int>
            {
                Code = ApiEnum.Success,
                Message = "修改成功",
                Data = 1
            };
        }
    }
}
